﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Web
{
    public partial class DatHang_GioHang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!IsPostBack)
            {
                LoadGioHang();
            }
        }
        protected void LoadGioHang()
        {
            
            string connectionstring = ConfigurationManager.ConnectionStrings["cnn"].ConnectionString;
            SqlConnection conn = new SqlConnection(connectionstring);

            string sql = "select *  from HoaDon";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();

            if (Request.QueryString["id"] != null)
            {
                string MaSo_SP = Request.QueryString["id"].ToString();
                if (Session["MaSo_HD"] != null)
                {
                    string mahd=Session["MaSo_HD"].ToString();
                    cmd.CommandText = "select * from HoaDon_CT where MaSo_SP=@MaSo_SP and MaSo_HD=@mahd";
                    cmd.Parameters.AddWithValue("@MaSo_SP", MaSo_SP);
                    cmd.Parameters.AddWithValue("@mahd", mahd);
                    SqlDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        reader.Close();
                        cmd.CommandText = "update HoaDon_CT set SoLuong=SoLuong+1 where MaSo_SP=@MaSo_SP and MaSo_HD=@mahd ";
                        cmd.Parameters.Clear();
                        cmd.Parameters.AddWithValue("@MaSo_SP", MaSo_SP);
                        cmd.Parameters.AddWithValue("@mahd", mahd);
                        cmd.ExecuteNonQuery();

                        
                        
                    }
                    else
                    {
                        reader.Close();
                        cmd.CommandText = "insert HoaDon_CT(MaSo_SP,SoLuong,MaSo_HD) values(@MaSo_SP,1,@mahd)";
                        cmd.Parameters.Clear();
                        cmd.Parameters.AddWithValue("@MaSo_SP", MaSo_SP);
                        cmd.Parameters.AddWithValue("@mahd", mahd);
                        cmd.ExecuteNonQuery();
                    }

                    cmd.CommandText = "select *,SoLuong*GiaBan as ThanhTien from SanPham join HoaDon_CT on SanPham.MaSo_SP=HoaDon_CT.MaSo_SP where MaSo_HD='" + mahd + "'";
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataTable dt2 = new DataTable();
                    adapter.Fill(dt2);

                    cmd.CommandText = "select sum(SoLuong*GiaBan) as tong from SanPham join HoaDon_CT on SanPham.MaSo_SP=HoaDon_CT.MaSo_SP where MaSo_HD='" + mahd + "' group by MaSo_HD";
                    int tong = (int)cmd.ExecuteScalar();

                    TongTT.Text = (tong).ToString();

                    Repeater3.DataSource = dt2.DefaultView;
                    Repeater3.DataBind();
                    cmd.ExecuteNonQuery();
                }
                else
                {
                    //cmd.CommandText = "insert HoaDon(MaSo_HD,NgayDatHang,XacNhan,MaSo_KH) values('',(select GETDATE()),'False',select MaSo_KH from KhachHang where TenDangNhap_KH=@TenDangNhap_KH)";
                    //cmd.Parameters.Clear();
                    //cmd.Parameters.AddWithValue("@TenDangNhap_KH", Session["username"].ToString());

                    cmd.CommandText = "insert HoaDon(MaSo_HD,NgayDatHang,XacNhan,MaSo_KH) values('',(select GETDATE()),'False','')";
                    cmd.ExecuteNonQuery();
                    cmd.CommandText = "select top (1) MaSo_HD from HoaDon order by MaSo_HD desc";
                    Session["MaSo_HD"] = (string)cmd.ExecuteScalar();
                    string mahd2=Session["MaSo_HD"].ToString();
                    cmd.CommandText = "insert HoaDon_CT(MaSo_SP,SoLuong,MaSo_HD) values(@MaSo_SP,1,@mahd)";
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddWithValue("@MaSo_SP", MaSo_SP);
                    cmd.Parameters.AddWithValue("@mahd", mahd2);
                    cmd.ExecuteNonQuery();
                    cmd.CommandText = "select *,SoLuong*GiaBan as ThanhTien from SanPham join HoaDon_CT on SanPham.MaSo_SP=HoaDon_CT.MaSo_SP where MaSo_HD='" + mahd2 + "'";
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    adapter.Fill(dt);
                    cmd.CommandText = "select sum(SoLuong*GiaBan) as Tong from SanPham join HoaDon_CT on SanPham.MaSo_SP=HoaDon_CT.MaSo_SP where MaSo_HD='"+mahd2+"' group by MaSo_HD";
                    int Tong = (int)cmd.ExecuteScalar();
                    
                    TongTT.Text = (Tong).ToString();

                    Repeater3.DataSource = dt.DefaultView;
                    Repeater3.DataBind();
                }

            }
        }

       

        protected void buttonTHDT_Click(object sender, EventArgs e)
        {
            Response.Redirect("TienHanhDatHang.aspx");
        }

        

        protected void buttonTTMH_Click1(object sender, EventArgs e)
        {
            Response.Redirect("ShowSanPhamVang.aspx");
        }
    }
}